package com.dsg.controller.apply;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.apply.DsgApplyDiscount;
import com.dsg.entity.dto.CardDiscountDto;
import com.dsg.service.apply.IDsgApplyDiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 折扣資料 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/apply/dsgApplyDiscount")
@Api(description = "折扣資料")
public class DsgApplyDiscountController extends BaseController {

    @Autowired
    IDsgApplyDiscountService service;

    @PostMapping(value = "/batchUpdate")
    @ApiOperation("批量修改折扣")
    public AjaxResult batchUpdate(String cardIds, String discountType, Integer cheepAmount, Integer costlyAmount) {
        LambdaUpdateWrapper<DsgApplyDiscount> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(DsgApplyDiscount::getCardId, Arrays.asList(cardIds.split(",")));
        wrapper.eq(DsgApplyDiscount::getDiscountType, discountType);
        DsgApplyDiscount discount = new DsgApplyDiscount();
        discount.setCheepAmount(cheepAmount);
        discount.setCostlyAmount(costlyAmount);
        service.update(discount, wrapper);
        return success();
    }

    @GetMapping(value = "/cardDiscount")
    @ApiOperation("客戶資料-折扣記錄-油咭折扣記錄")
    @ApiResponses({
            @ApiResponse(response = CardDiscountDto.class, code = 1, message = "ok")
    })
    public AjaxResult cardDiscount(String cardOids, String effDateBegin, String effDateEnd) {
        return success(service.cardDiscount(cardOids, effDateBegin, effDateEnd));
    }

    @GetMapping(value = "/specialDiscount")
    @ApiOperation("客戶資料-特別折扣")
    @ApiResponses({
            @ApiResponse(response = DsgApplyDiscount.class, code = 1, message = "ok")
    })
    public AjaxResult specialDiscount(String cardId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("CARD_ID", cardId);
        wrapper.in("DISCOUNT_TYPE", DsgApplyDiscount.DISCOUNT_TYPE_2, DsgApplyDiscount.DISCOUNT_TYPE_3, DsgApplyDiscount.DISCOUNT_TYPE_4);
        List<DsgApplyDiscount> list = service.list(wrapper);
        return success(list);
    }

    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgApplyDiscount applyDiscount) {
        applyDiscount.setOid(UUIDGenerator.getUUID());
        boolean falg = service.save(applyDiscount);
        if (falg) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @PutMapping(value = "/saveOrUpdate")
    @ApiOperation("新增或更新")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveOrUpdate(@RequestBody List<DsgApplyDiscount> applyDiscountList, @RequestParam String cusNo) {
//        for (int i = 0; i < applyDiscountList.size(); i++) {
//            if (StringUtil.isEmpty(applyDiscountList.get(i).getAppNo())) {
//                return fail("APP_NO不能为空");
//            }
//        }

        service.saveDiscount(cusNo, applyDiscountList);
        return json(MessageConstants.SSO_STATUS_SUCCESS, applyDiscountList);
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public AjaxResult delete(String id) {
        Boolean flag = service.removeById(id);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    @GetMapping(value = "/{mainNo}/getByBill")
    @ApiOperation("通过申请单查询单条数据")
    public AjaxResult getByBill(@PathVariable String mainNo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("MAIN_NO", mainNo);
        DsgApplyDiscount applyDiscount = service.getOne(wrapper);
        return json(MessageConstants.SSO_STATUS_SUCCESS, applyDiscount);
    }
}
